#pragma once
#include <iostream>
#include <string>
#include <ctime>
#include <unistd.h>
#include <sys/types.h>

using namespace std;

typedef void(*work_t)(int);
typedef void(*task_t)(int, pid_t);

void task1(int fd, pid_t id)
{
    cout << "fd: " << fd << " id: " << id << " 任务1" << endl;
}
void task2(int fd, pid_t id)
{
    cout << "fd: " << fd << " id: " << id << " 任务2" << endl;
}
void task3(int fd, pid_t id)
{
    cout << "fd: " << fd << " id: " << id << " 任务3" << endl;
}

task_t task[3] = {task1, task2, task3};

uint32_t NextTask()
{
    return rand() % 3;
}

void worker(int fd)
{
    while(true)
    {
        uint32_t command_code = 0;
        int n = read(fd, &command_code, sizeof(command_code));
        if(n == sizeof(command_code))
        {
            if(command_code >= 3)
                continue;
            task[command_code](fd, getpid());
        }
        else if(n == 0)
        {
            cout << "sub process: " << getpid() <<" quit..." << endl; 
            break;
        }
    }
}
